package com.forezp.thinking.chapter18.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by forezp on 2017/12/11.
 */
public class FileChannelCopy {

    private final static int BSIZE = 1024;

    public static void copy(String inFileName, String outFileName) throws IOException {
        File inFile = new File(inFileName);
        File outFile = new File(outFileName);
        FileChannel in = new FileInputStream(inFile).getChannel();
        FileChannel out = new FileInputStream(outFile).getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(BSIZE);
        while ((in.read(byteBuffer)) != -1) {
            byteBuffer.flip();//准备写
            out.write(byteBuffer);
            byteBuffer.clear();//准备读
        }
    }
}
